import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/edu_get_user_by_id_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/get_user_info_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/education/personal_profile/basic/basic_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/education/personal_profile/cert/cert_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/education/personal_profile/face/face_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/education/personal_profile/real_name/name_controller.dart';

///个人档案
/// 如果叫 `PersonalProfile` 可能与其他组件重复
class EducationProfileController extends GetxController {
  int initPage = 0;

  EduGetUserByIdEntity? entity;

  void getDetailsData({void Function()? callback, bool force = false}) async {
    if (!force && entity != null) return;
    final resp = await HttpUtil().generateGetRequest<GetUserInfoEntity>(Interface.getUserInfo).execute();
    resp.onSuccess((result) async {
      if (result.data != null) {
        final res = await HttpUtil().generateGetRequest<EduGetUserByIdEntity>(
          Interface.getEnterpriseUserById,
          queryParameters: {'userId': result.data!.userId},
        ).execute();
        res.onSuccess((result) {
          final data = result.data;
          if (data != null) {
            entity = data;
            callback?.call();
            update();
          }
        });
      }
    });
  }

  Future<void> updateProfile() async {
    await HttpUtil().generatePostRequest(Interface.updateUser, data: entity).execute();
  }

  @override
  void onInit() {
    super.onInit();
    getDetailsData(callback: () {
      Get.put(EducationProfileBasicController()).onProfileChanged();
      Get.put(EducationProfileNameController()).onProfileChanged();
      Get.put(EducationProfileFaceController()).onProfileChanged();
      Get.put(EducationProfileCertController()).onProfileChanged();
    });
  }
}
